﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Xilium.CefGlue;

namespace NFinal.Browser
{
    public static class Program
    {
        public static string url;
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            CefRuntime.Load();
            var mainArgs = new CefMainArgs(new string[] { });
            var exitCode = CefRuntime.ExecuteProcess(mainArgs, null,IntPtr.Zero);
            if (exitCode != -1)
                return;
            var settings = new CefSettings
            {
                SingleProcess = false,
                MultiThreadedMessageLoop = true,
                LogSeverity = CefLogSeverity.Disable,
                Locale = "zh-CN",
                UserAgent = "Mozilla/5.0 (X11; CrOS i686 3912.101.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36"
            };
            CefRuntime.Initialize(mainArgs, settings, null, IntPtr.Zero);
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            if (!settings.MultiThreadedMessageLoop)
            {
                Application.Idle += (sender, e) => { CefRuntime.DoMessageLoopWork(); };
            }


            var options = new Options();
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Browser browserForm = new Browser();
            if (CommandLine.CommandLineParser.Default.ParseArguments(args, options))
            {
                browserForm.Width = options.width;
                browserForm.Height = options.height;
            }
            browserForm.Text = Uri.UnescapeDataString(options.Title);
            Program.url = options.url;
            Application.Run(browserForm);
            CefRuntime.Shutdown();
        }
    }
}
